package com.yanqu.road.server.cmd.activity.seacraftsouthsea;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.seacraft.SeacraftSouthSeaModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.SeacraftSouthSeaCommonMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Description
 * @Author cwq
 * @Data 2022/2/12 16:51
 */
@Cmd(code = GameProtocol.S_SEACRAFT_SOUTH_SEA_CHALLENGE_FROM_CROSS, desc = " 海上争霸 南海丝路 挑战")
public class SeacraftSouthSeaChallengeFromCrossCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SeacraftSouthSeaProto.SeacraftSouthSeaChallengeCrossToGameMsg crossToGameMsg = SeacraftSouthSeaProto.SeacraftSouthSeaChallengeCrossToGameMsg.parseFrom(packet.getBytes());
        if(crossToGameMsg.getType() == 2){
            // 需求预扣除
            if(crossToGameMsg.getGoldNum() > 0){
                Property property = new Property(GameConfig.GAME_MONEY_INGOTS, crossToGameMsg.getGoldNum());
                player.getModule(PreReduceModule.class).restorePreReduce(property);
            }
        }
        SeacraftSouthSeaProto.SeacraftSouthSeaChallengeRespMsg stcMsg = crossToGameMsg.getStcMsg();
        if(stcMsg.getRet() == 0){
            // 扣元宝
            if(crossToGameMsg.getType() == 2 && stcMsg.getConsumeGoldNum() > 0){
                // 扣道具
                Property consume = new Property(GameConfig.GAME_MONEY_INGOTS,stcMsg.getConsumeGoldNum());
                player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.SeacraftActivity, eLogMoneyType.SeacraftActivityOneKeyChallengeCost);
            }
            // 加道具
            Property winStreakReward = new Property();
            for (SeacraftSouthSeaProto.SeacraftSouthSeaBattleDetail item : stcMsg.getBattleDetailListList()){
                // 连胜奖励
                winStreakReward.addProperty(PropertyHelper.parseStringToProperty(item.getWinStreakReward()));
            }
            // 连胜奖励
            if(winStreakReward.getGoods().size() > 0){
                player.getModule(CurrencyModule.class).addCurrency(winStreakReward, eLogMoneyType.SeacraftActivity,eLogMoneyType.SeacraftActivityWinStreakReward);
            }
            // 对决奖励
            player.getModule(SeacraftSouthSeaModule.class).battleEnd(stcMsg.getResult());
            // 同步积分
            player.getModule(SeacraftSouthSeaModule.class).synUserScore(stcMsg.getAllScore());
        }
        player.sendPacket(ClientProtocol.U_SEACRAFT_SOUTH_SEA_CHALLENGE, stcMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
